<?php

namespace App\Http\Controllers;

use App\Http\Responses\RespCode;
use App\Http\Validates\ValidateInterface;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     *  拦截 全局 验证错误！！.
     * @param Request $request
     * @param Validator $validator
     * @throws ValidationException
     */
    protected function throwValidationException(Request $request, Validator $validator)
    {
        throw new ValidationException($validator, new JsonResponse([
            'code' => RespCode::FAILED,
            'msg' => '字段参数验证错误：参数【' . $validator->errors()->keys()[0] . '】错误：' . $validator->errors()->first()
        ]));
    }

    /**
     * @param ValidateInterface $validate // 重写方法 参数必须一致
     * @param Request $request
     * @return array
     * @throws ValidationException
     */
    public function makeValidate(ValidateInterface $validate, Request $request): array
    {
        return parent::validate($request, $validate->getRules(), $validate->getMessages());
    }

}
